﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace Eugenhome.Atrium.Site.Web
{
    /// <summary>
    /// 
    /// </summary>
    public partial class login : System.Web.UI.Page
    {
        /// <summary></summary>
        private Login _login = new Login();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpdateView(_login.Valid);

                if (Request.QueryString["url"] != null && !Request.QueryString["url"].Trim().Equals(string.Empty))
                {
                    parentUrl.Value = Request.QueryString["url"];
                }
            }

            btnLogoutCancel.Visible = _login.SignFileExists && !parentUrl.Value.Equals(string.Empty);
            btnLogout.Visible = _login.SignFileExists;

            lblLogin.Text = "Anmelden:";
            lblLogin.ForeColor = Color.Black;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logged"></param>
        private void UpdateView(bool logged)
        {
            if (logged && !parentUrl.Value.Equals(string.Empty))
            {
                Response.Redirect(parentUrl.Value);
            }
            else
            {
                pnlLogin.Visible = _login.SignFileExists ? !logged : false;
                pnlLogout.Visible = !pnlLogin.Visible;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            bool result = _login.Check(txtPass.Text);

            if (!result)
            {
                lblLogin.Text = "Falsches Kennwort:";
                lblLogin.ForeColor = Color.Brown;
            }

            UpdateView(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNewLogin_Click(object sender, EventArgs e)
        {
            if (_login.Valid)
            {
                if (!_login.SetNewPrivateKey(txtNewPass.Text))
                {
                    lblLogin.Text = "Setzen des neuen Kennwort ist fehlgeschlagen!";
                    lblLogin.ForeColor = Color.Brown;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogoutCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(parentUrl.Value);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogout_Click(object sender, EventArgs e)
        {
            _login.Logout();

            UpdateView(false);
        }
    }
}